#include "gc.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "Path.h"

void vis_truth_path (std::string truthPathName, int truthPathNo, 
		     std::string truthImageFileTemplate, 
		     std::string bgImageFileTemplate, 
		     std::string outputImageFileTemplate)
{
  ListOfPaths truthPathList;
  truthPathList.readXMLPaths(truthPathName.c_str());
  Path truthPath = truthPathList.get(truthPathNo);
  int truthStartSliceNo = truthPath.lastSlice();
  int truthEndSliceNo = truthPath.firstSlice();
  int truthSliceNum = truthEndSliceNo - truthStartSliceNo + 1;
  RgbImage::Pointer rgbImages[truthSliceNum];
  for (int truthSliceNo = truthStartSliceNo; truthSliceNo <= truthEndSliceNo; truthSliceNo++) {
    char fn[200];
    sprintf(fn, bgImageFileTemplate.c_str(), truthSliceNo);
    rgbImages[truthSliceNo - truthStartSliceNo] = GrayToRgb(fn);
  }
  for (int truthIndex = truthPath.size() - 1; truthIndex >= 0; truthIndex--) {
    int sliceNo = truthPath.getSliceAtIndex(truthIndex);
    int label = truthPath.getLabelAtIndex(truthIndex);
    char fn[200];
    sprintf(fn, truthImageFileTemplate.c_str(), sliceNo);
    LabelImage::Pointer truthImage = readLabelImage(fn);
    Component component;
    for (itk::ImageRegionConstIteratorWithIndex<LabelImage> 
	   itr(truthImage, truthImage->GetLargestPossibleRegion()); 
	 !itr.IsAtEnd(); ++itr) {
      if (itr.Get() == label) {
	Point point(itr.GetIndex());
	component.points.push_back(point);
      }
    }
    RgbPixel fgPixel;
    fgPixel.Set(0, 255, 0);
    fillComponent(component, rgbImages[sliceNo - truthStartSliceNo], fgPixel, 0.2);
  }
  for (int truthSliceNo = truthStartSliceNo; truthSliceNo <= truthEndSliceNo; truthSliceNo++) {
    char fn[200];
    sprintf(fn, outputImageFileTemplate.c_str(), truthSliceNo);
    writeRgbImage(rgbImages[truthSliceNo - truthStartSliceNo], fn);
  }
}



int main (int argc, char* argv[]) 
{
  int argNo = 1;
  std::string truthPathName = argv[argNo++];
  int truthPathNo = atoi(argv[argNo++]);
  std::string truthImageFileTemplate = argv[argNo++];
  std::string bgImageFileTemplate = argv[argNo++];
  std::string outputImageFileTemplate = argv[argNo++];
  vis_truth_path(truthPathName, truthPathNo, truthImageFileTemplate, 
		 bgImageFileTemplate, outputImageFileTemplate);
  return 0;
}
